const query=require('../models');

module.exports = {
    get(req,res) {
        let { current, pageSize } = req.query;
        current = Number(current);
        pageSize = Number(pageSize);
        let start = (current - 1) * pageSize;
        const sql = `SELECT course_id courseNumber,course_name courseName,DATE_FORMAT(course_createtime,'%Y-%m-%d') courseCreateTime,DATE_FORMAT(course_starttime,'%Y-%m-%d') courseStartTime,course_inroduction courseIntroduction,course_content courseContent,course_duration courseDuration, course_class courseClass,course_teacher courseProfessor,course_image courseImage,course_address courseAddress
        FROM courses
        WHERE is_del = 1
        LIMIT ?,?`
        

        query(sql,[start,pageSize])
         .then(function(result) {
             res.json({
                code:'200',
                msg: '所有课程渲染成功',
                data: result
            })
         })
         .catch(function(err) {
             res.json({
                 code:'1001',
                 msg:'所有课程渲染失败',
                 err
             })
         })
    },
    total(req,res) {
        const sql = `SELECT COUNT(*) total
        FROM courses WHERE is_del = 1
        `
        query(sql,null) 
         .then(function(result) {
             res.json({
                 code:'200',
                 msg:'总数请求成功',
                 data: result[0]
             })
         })
         .catch(function(err) {
            res.json({
                code:'1001',
                msg:'总数请求失败',
                err
            })
         })
    },
    del(req,res) {
        const sql = `UPDATE courses SET is_del=0 WHERE course_id IN (?)
        `
        query(sql,[req.query.courseId])
         .then(function(result) {
            res.json({
                code:'200',
                msg:'删除成功',
                data:result
            })
         })
    },
    getById(req,res) {
        const sql = `SELECT course_id courseNumber 
        FROM courses`
        query(sql,null)
         .then(function(result) {
             res.json({
                 code:'200',
                 msg:'id请求成功',
                 data:result
             })
         })
         .catch(function(err) {
            res.json({
                code:'1001',
                msg:'id请失败',
                err
            })
         })
    },
    add(req,res) {
        console.log('新添加的数据',req.body)
        const sql = `INSERT INTO courses VALUES(DEFAULT,?,?,?,?,?,?,?,?,?,?,1)`
        let {courseName,courseIntroduction,courseContent,courseTeacher,courseImage,courseNumber,courseCreateTime,courseStartTime,courseDuration,courseClass,courseAddress} =req.body
        console.log(req.body)
        query(sql,[courseName,courseCreateTime,courseStartTime,courseIntroduction,courseContent,courseDuration,courseClass,courseTeacher,courseImage,courseAddress])
         .then(function(result) {
             console.log('新增之后的结果：',result)
             res.json({
                 code:'200',
                 msg:'添加成功',
             })
         })
         .catch(function(err) {
             console.log('shibai')
             res.json({
                 code:'1001',
                 msg:'添加失败',
                 err
             })
         })
    },
    edit(req,res) {
        let {courseName,courseIntroduction,courseContent,courseTeacher,courseImage,courseNumber,courseCreateTime,courseStartTime,courseDuration,courseClass,courseAddress} =req.body
        const sql= `UPDATE courses SET course_name=?,course_createtime=?,course_starttime=?,course_inroduction=?,course_content=?,course_duration=?,course_class=?,course_teacher=?,course_image=?,course_address=? WHERE course_id=?`

        query(sql,[courseName,courseCreateTime,courseStartTime,courseIntroduction,courseContent,courseDuration,courseClass,courseTeacher,courseImage,courseAddress,courseNumber])
         .then(function(result){
             res.json({
                 code:'200',
                 msg:'编辑成功'
             })
         })
         .catch(function(err) {
             res.json({
                 code:'1001',
                 msg:'编辑失败',
                 err
             })
         })
    },
    selectId(req,res) {
        const editId = req.query.editId
        const sql = `SELECT course_id courseNumber,course_name courseName,DATE_FORMAT(course_createtime,'%Y-%m-%d') courseCreateTime,DATE_FORMAT(course_starttime,'%Y-%m-%d') courseStartTime,course_inroduction courseIntroduction,course_content courseContent,course_duration courseDuration, course_class courseClass,course_teacher courseProfessor,course_image courseImage,course_address courseAddress
        FROM courses
        WHERE course_id = ?`
        query(sql,[editId])
         .then(function(result) {
             res.json({
                 code:'200',
                 msg:'编辑id请求成功',
                 data:result[0]
             })
         })
         .catch(function(err) {
             res.json({
                 code:'1002',
                 msg:'编辑id请求失败',
                 err
             })
         })
    },
    view(req,res) {
        const viewId = req.query.viewId
        const sql = `SELECT course_id courseNumber,course_name courseName,DATE_FORMAT(course_createtime,'%Y-%m-%d') courseCreateTime,DATE_FORMAT(course_starttime,'%Y-%m-%d') courseStartTime,course_inroduction courseIntroduction,course_content courseContent,course_duration courseDuration, course_class courseClass,course_teacher courseProfessor,course_image courseImage,course_address courseAddress
        FROM courses
        WHERE course_id = ?`
        query(sql,[viewId])
        .then(function(result) {
            res.json({
                code:'200',
                msg:'查看id请求成功',
                data: result
            })
        })
        .catch(function(err) {
            res.json({
                code:'1002',
                msg:'查看id请求失败',
                err
            })
        })
    }

}